package com.example.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.example.R
import com.example.bus.AndroidJniControl

class SerialFragment : Fragment() {

    var  serial_fd = -1;

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_serial, container, false)
        val edit =  view.findViewById<EditText>(R.id.edit_serial)//编辑框

        view.findViewById<Button>(R.id.open_serial).setOnClickListener {
            serial_fd = AndroidJniControl.openSerial()//打开串口
            Log.i("demo serial_fd ",serial_fd.toString())
        }

        view.findViewById<Button>(R.id.send_serial).setOnClickListener {
            //往串口发送编辑框的内容
            AndroidJniControl.sendSerialData(serial_fd,edit.text.toString())
        }

        view.findViewById<Button>(R.id.close_serial).setOnClickListener {
            //关闭串口
            AndroidJniControl.closeSerial(serial_fd)
        }

        return view
    }
}